package com.wms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wms.domain.User;
import com.wms.mapper.UserMapper;
import com.wms.service.LoginService;
import com.wms.utils.JwtUtils;
import com.wms.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl extends ServiceImpl<UserMapper, User> implements LoginService {
    @Autowired
    UserMapper userMapper;

    @Override
    public LoginVo login(User user) {
        User loginInfo = userMapper.login(user);
        if (ObjectUtils.isEmpty(loginInfo)) {
            return null;
        }
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", loginInfo.getId());
        claims.put("username", loginInfo.getUsername());
        claims.put("empName", loginInfo.getEmpName());
        String token = JwtUtils.generate(claims);
        return new LoginVo(loginInfo.getId(), loginInfo.getUsername(), loginInfo.getEmpName(), token);
    }
}
